<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.01.
   家居公司套餐模型($$)*/

!defined('Q_PATH') && exit;

class JiajupackageModel extends CommonModel{

	static public function init__(){
		return array(
			'table_name'=>'jiajupackage',
			'check'=>array(
				'jiajupackage_sort'=>array(
					array('number',Q::L('序号只能是数字','__COMMON_LANG__@Common')),
				),
				'jiajupackage_title'=>array(
					array('require',Q::L('套餐标题不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',100,Q::L('套餐标题不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajupackage_price'=>array(
					array('require',Q::L('套餐价格不能为空','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajupackage_firstprice'=>array(
					array('require',Q::L('套餐原价格不能为空','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajupackage_thumb'=>array(
					array('require',Q::L('套餐缩略图不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',100,Q::L('套餐缩略图不能超过100个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
				'jiajupackage_intro'=>array(
					array('require',Q::L('套餐描述不能为空','__APPJIAJU_COMMON_LANG__@Model')),
					array('max_length',250,Q::L('套餐描述不能超过250个字符','__APPJIAJU_COMMON_LANG__@Model')),
				),
			),
		);
	}

	static function F(){
		$arrArgs=func_get_args();
		return ModelMeta::instance(__CLASS__)->findByArgs($arrArgs);
	}

	static function M(){
		return ModelMeta::instance(__CLASS__);
	}

	protected function beforeSave_(){
		$this->jiajupackage_title=C::text($this->jiajupackage_title);
		$this->jiajupackage_intro=C::text($this->jiajupackage_intro);
		$this->jiajupackage_thumb=C::text($this->jiajupackage_thumb);
		$this->jiajupackage_username=C::text($this->jiajupackage_username);
		$this->jiajupackage_editusername=C::text($this->jiajupackage_editusername);
		
		if($this->jiajupackage_sort<0){
			$this->jiajupackage_sort=0;
		}
		if($this->jiajupackage_sort>999){
			$this->jiajupackage_sort=999;
		}
	}

	public function saveData($arrCompany){
		// 启动事务
		$bRollback=false;
		$oDb=$this->getDb()->getConnect();
		$oDb->startTransaction();

		if(empty($arrCompany['jiajucompany_id'])){
			$arrCompany=Model::F_('jiajucompany','jiajucompany_id=?',$arrCompany)->setColumns('jiajucompany_id,site_id,province_id,city_id')->getOne();
		}

		// 商家&城市
		$this->jiajucompany_id=$arrCompany['jiajucompany_id'];
		$this->site_id=$arrCompany['site_id'];
		$this->province_id=$arrCompany['province_id'];
		$this->city_id=$arrCompany['city_id'];

		// 添加人
		$this->user_id=$GLOBALS['___login___']['user_id'];
		$this->jiajupackage_username=$GLOBALS['___login___']['user_name'];
		$this->jiajupackage_editusername=$GLOBALS['___login___']['user_name'];
		$this->jiajupackage_edituserid=$GLOBALS['___login___']['user_id'];
		$this->save();
		if($this->isError()){
			$bRollback=true;
		}

		// 保存套餐内容
		if($bRollback===false){
			$oJiajupackagecontent=new JiajupackagecontentModel();
			$oJiajupackagecontent->jiajupackage_id=$this->jiajupackage_id;
			
			// 套餐内容
			$arrData=array();
			for($nI=0;$nI<=11;$nI++){
				$arrData[$nI][0]=$_POST['packagebg'][$nI];
				$arrData[$nI][1]=$_POST['packagecontent'][$nI];
			}
			$oJiajupackagecontent->jiajupackage_content=json_encode($arrData);

			$oJiajupackagecontent->save();
			if($oJiajupackagecontent->isError()){
				$bRollback=true;
				$this->_sErrorMessage=$oJiajupackagecontent->getErrorMessage();
			}
		}

		if($bRollback===false){
			$oDb->commit();
			return true;
		}else{
			$oDb->rollback();
			return false;
		}
	}

	public function updateData($arrCompany){
		// 启动事务
		$bRollback=false;
		$oDb=$this->getDb()->getConnect();
		$oDb->startTransaction();

		if(empty($arrCompany['jiajucompany_id'])){
			$arrCompany=Model::F_('jiajucompany','jiajucompany_id=?',$arrCompany)->setColumns('jiajucompany_id,site_id,province_id,city_id')->getOne();
		}

		// 商家&城市
		$this->site_id=$arrCompany['site_id'];
		$this->province_id=$arrCompany['province_id'];
		$this->city_id=$arrCompany['city_id'];
		$this->jiajucompany_id=$arrCompany['jiajucompany_id'];

		// 编辑人
		$this->jiajupackage_editusername=$GLOBALS['___login___']['user_name'];
		$this->jiajupackage_edituserid=$GLOBALS['___login___']['user_id'];

		// 修改后提交
		if($this->jiajupackage_status==1 && APP_NAME!=='admin'){
			$this->jiajupackage_status=11;
		}
		$this->save('update');
		if($this->isError()){
			$bRollback=true;
		}

		// 保存套餐内容
		if($bRollback===false){
			$oJiajupackagecontent=JiajupackagecontentModel::F('jiajupackage_id=?',$this->jiajupackage_id)->getOne();
			if(empty($oJiajupackagecontent['jiajupackage_id'])){
				$bRollback=true;
			}else{
				// 套餐内容
				$arrData=array();
				for($nI=0;$nI<=11;$nI++){
					$arrData[$nI][0]=$_POST['packagebg'][$nI];
					$arrData[$nI][1]=$_POST['packagecontent'][$nI];
				}
				$oJiajupackagecontent->jiajupackage_content=json_encode($arrData);

				$oJiajupackagecontent->save('update');
				if($oJiajupackagecontent->isError()){
					$bRollback=true;
					$this->_sErrorMessage=$oJiajupackagecontent->getErrorMessage();
				}
			}
		}

		if($bRollback===false){
			$oDb->commit();
			return true;
		}else{
			$oDb->rollback();
			return false;
		}
	}

}
